<template>
  <div class="app-container">
    <!-- 头部表单 -->
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary">起始</el-button>
          <el-date-picker
            v-model="formInline.task_start"
            type="date"
            style="width:150px"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary">截止</el-button>
          <el-date-picker
            v-model="formInline.task_end"
            type="date"
            style="width:150px"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="pickerOptions"
          />
        </el-form-item>
        <el-form-item style="width:90px">
          <el-select v-model="formInline.task_level" placeholder="所有">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="width:90px">
          <el-select v-model="formInline.task_type" placeholder="所有">
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.keyword" style="width:200px" placeholder="关键字:工单编号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary">排序</el-button>
        </el-form-item>
        <el-form-item style="width:90px">
          <el-select v-model="formInline.order" placeholder="所有">
            <el-option
              v-for="item in options5"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="width:90px">
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-card class="mt20">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        size="mini"
        border
        @row-click="handleRowClick"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="35"
          align="center"
        />
        <el-table-column
          prop="task_num"
          label="工单编号"
          align="center"
          width="170"
        />
        <el-table-column
          prop="task_start"
          label="生成日期"
          align="center"
          width="150"
        />
        <el-table-column
          prop="task_start"
          label="结束日期"
          align="center"
          width="150"
        />
        <el-table-column
          prop="task_title"
          label="累计用时"
          align="center"
          width="170"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="task_level"
          label="级别"
          align="center"
          width="170"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.task_level=='一般'">{{ scope.row.task_level }}</el-tag>
            <el-tag v-if="scope.row.task_level=='重要'" type="success">{{ scope.row.task_level }}</el-tag>
            <el-tag v-if="scope.row.task_level=='紧急'" type="danger">{{ scope.row.task_level }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="task_stat"
          label="状态"
          align="center"
          width="145"
        />
        <el-table-column
          prop="work_num"
          label="发起人"
          align="center"
          width="145"
        />
        <el-table-column
          prop="group_user"
          label="执行班组/人"
          align="center"
          width="95"
        />
        <el-table-column
          prop="task_type"
          label="类别"
          align="center"
          width="170"
        />
        <el-table-column
          label="操作"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <!-- 进度 -->
            <i style="font-size: 28px; color: #409EFF" class="el-icon-s-operation" @click="pro(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <!-- 底部分页 -->
    <div class="currentPage">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSixe"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totals"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handelPrevChange"
        @next-click="handelNextChange"
      />
    </div>

    <!-- 进度弹框 -->
    <el-dialog v-dialogDrag :title="proTitle" center :visible.sync="dialogTableVisible2">
      <div slot="title" class="header-title">
        <i class="el-icon-edit" />
        <span class="seeTit">{{ proTitle }}</span>
      </div>
      <div>
        <Progress :pro-dtetail="proDtetail" :num="num" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="dialogTableVisible2 = false">关闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { orderList, groupList, workerAllList, progre, useCars } from '@/api/order'
import Progress from './components/progress.vue'
export default {
  name: 'index',
  components: {
    Progress
  },
  data() {
    return {
      formInline: {
        offset: '',
        limit: '',
        task_start: '',
        task_end: '',
        task_level: '',
        task_stat: '',
        task_type: '',
        keyword: '',
        order: ''
      },
      options1: [{
        value: '',
        label: '所有'
      }, {
        value: '一般',
        label: '一般'
      }, {
        value: '重要',
        label: '重要'
      }, {
        value: '紧急',
        label: '紧急'
      }],
      options3: [{
        value: '',
        label: '所有'
      }, {
        value: '1',
        label: '巡检'
      }, {
        value: '2',
        label: '试验'
      }, {
        value: '3',
        label: '报修'
      }, {
        value: '4',
        label: '抢修'
      }],
      options5: [{
        value: '1',
        label: '降序'
      }, {
        value: '2',
        label: '升序'
      }],
      tableData: [],
      multipleSelection: [], // 列表选中的数据
      currentPage: 1,
      pageSixe: 10,
      totals: 0,
      sendTitle: '派单',
      dialogTableVisible: false, // 派单
      pickerOptions: { // 日期选择只能选今天之前的日期
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      proTitle: '工单进度',
      dialogTableVisible2: false, // 进度
      proDtetail: '',
      num: ''
    }
  },
  mounted() {
    this.getdatatime()
    this.getSevent()
  },
  created() {
    this.formInline.task_level = this.options1[0].value
    this.formInline.task_type = this.options3[0].value
    this.formInline.order = this.options5[0].label
    this.getMaintainList()
  },
  methods: {
    time(date) {
      var y = date.getFullYear()
      var m = date.getMonth() + 1
      m = m < 10 ? '0' + m : m
      var d = date.getDate()
      d = d < 10 ? '0' + d : d
      var h = date.getHours()
      h = h < 10 ? '0' + h : h
      var minute = date.getMinutes()
      minute = minute < 10 ? '0' + minute : minute
      var second = date.getSeconds()
      second = second < 10 ? '0' + second : second
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
    },
    // 默认显示今天
    getdatatime() {
      this.formInline.task_end = this.time(new Date())
      console.log(this.formInline.task_end)
    },
    // 默认一周前
    getSevent() {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
      this.formInline.task_start = this.time(date)
    },
    // 获取工单列表
    getMaintainList() {
      console.log(7777777)
      console.log(this.formInline)
      this.formInline.offset = this.currentPage
      this.formInline.limit = this.pageSixe
      const arr = this.formInline
      arr.task_level = ''
      arr.task_status = ''
      arr.task_type = ''
      arr.order = ''
      console.log(this.formInline)
      orderList(arr).then(res => {
        if (res.code === 200) {
          const arr = res.data.list
          arr.forEach(item => {
            if (item.task_status == 0) {
              item.task_stat = '待派单'
            }
            if (item.task_status == 1) {
              item.task_stat = '已派单'
            }
            if (item.task_status == 2) {
              item.task_stat = '已接受'
            }
            if (item.task_status == 3) {
              item.task_stat = '已拒绝'
            }
            if (item.task_status == 4) {
              item.task_stat = '已超时'
            }
            if (item.task_status == 5) {
              item.task_stat = '已完成'
            }
          })
          this.tableData = arr
          this.totals = res.data.total
        }
      })
    },
    // 搜索
    search() {
      this.formInline.offset = this.currentPage
      this.formInline.limit = this.pageSixe
      console.log(this.formInline)
      orderList(this.formInline).then(res => {
        if (res.code === 200) {
          const arr = res.data.list
          arr.forEach(item => {
            if (item.task_status == 0) {
              item.task_stat = '待派单'
            }
            if (item.task_status == 1) {
              item.task_stat = '已派单'
            }
            if (item.task_status == 2) {
              item.task_stat = '已接受'
            }
            if (item.task_status == 3) {
              item.task_stat = '已拒绝'
            }
            if (item.task_status == 4) {
              item.task_stat = '已超时'
            }
            if (item.task_status == 5) {
              item.task_stat = '已完成'
            }
          })
          this.tableData = arr
          this.totals = res.data.total
        }
      })
    },

    // 获取班组列表
    getGroup() {
      groupList({ offset: 0, limit: 1, pageType: 'all' }).then(res => {
        // this.group_user = res.data.groups
        const arr = res.data.groups
        arr.forEach(item => {
          item.longName = item.group_name + '(负责人：' + item.group_admin + ')' + '(' + item.status_name + ')'
        })
        this.group_user = arr
      })
    },
    // 获取运维人员列表
    getGroups() {
      workerAllList({ guard_name: '', limit: 10, offset: 0, order: 'ascending', sort: 'created_at' }).then(res => {
        // this.group_user = res.data.groups
        const arr = res.data.admins
        arr.forEach(item => {
          item.longName = item.name + '(' + item.status_name + ')'
        })
        this.g_users = arr
      })
    },
    // 表单选中
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击行触发，选中或不选中复选框
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },

    // 进度
    pro(a, b) {
      this.dialogTableVisible2 = true
      const data = {
        task_num: b.task_num
      }
      progre(data).then(res => {
        this.proDtetail = res.data.list
        const nums = this.proDtetail[this.proDtetail.length - 1].task_status
        if (nums == 1) {
          const arr1 = [
            {
              status_name: '已派单',
              created_at: ''
            },
            {
              status_name: '已接受',
              created_at: ''
            },
            {
              status_name: '已完成',
              created_at: ''
            },
            {
              status_name: '已确认',
              created_at: ''
            }
          ]
          this.proDtetail = arr1
          this.num = 1
        }

        if (nums == 2) {
          this.num = this.proDtetail.length
          const arr2 = [
            {
              status_name: '已完成',
              created_at: ''
            },
            {
              status_name: '已确认',
              created_at: ''
            }
          ]
          this.proDtetail = this.proDtetail.concat(arr2)
        }
        if (nums == 3) {
          this.num = this.proDtetail.length
          const arr3 = [
            {
              status_name: '已确认',
              created_at: ''
            }
          ]
          this.proDtetail = this.proDtetail.concat(arr3)
        }
        if (nums == 4) {
          this.num = this.proDtetail.length
        }
      }
      )
    },

    handleSizeChange(val) {
      this.pageSixe = val
      this.getMaintainList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getMaintainList()
    },
    handelPrevChange(val) {
      this.currentPage = val
      this.getMaintainList()
    },
    handelNextChange(val) {
      this.currentPage = val
      this.getMaintainList()
    }
  }
}
</script>

<style lang="scss" scoped>
  .btn{
    position: relative;
    // top:-2px;
    left: 1px;
  }
   .currentPage{
    // display: flex;
    position: fixed;
    bottom: 0;
    right: 0;
    // flex-direction: row-reverse;
    // margin-top: 20px;
  }
  /deep/.el-form-item{
    margin-right: 0;
  }
    .header-title{
    .seeTit{
      padding-left: 10px;
    }
  }
  /deep/.el-dialog__header{
    background-color: #263445;
    color: #fff;
  }
  .dialog-footer{
        text-align: right;

  }
  /deep/.el-dialog__footer{
    text-align: right;
  }
  .i1{
    color:rgb(119, 117, 117);
  }

</style>
